let selSector = document.getElementById('selSector');
let selWorker = document.getElementById('selWorker');
let spSector = document.getElementById('spSector');
let spWorker = document.getElementById('spWorker');

const BASE_URL = 'https://api.huhuiyu.top';

//#region 获取部门信息
function querySector() {
  let promise = axios({
    url: BASE_URL + '/link/departments',
    method: 'get',
    data: '',
  });
  promise
    .then((resp) => {
      let info = resp.data;
      if (!info.success) {
        alert(info.message);
        return;
      }
      selSector.innerHTML = '';
      for (let i = 0; i < info.data.length; i++) {
        let pro = info.data[i];
        let op = document.createElement('option');
        op.append(pro.deptName);
        op.setAttribute('value', pro.deptId);
        selSector.append(op);
      }
    })
    .catch((err) => {
      console.error(err);
    });
}
//#endregion


//#region 员工信息联动
function queryWorker() {
  spSector.innerHTML = selSector.value;
  let promise = axios({
    url: `${BASE_URL}/link/employees/${selSector.value}`,
    method: 'get',
    data: '',
  });
  promise
    .then((resp) => {
      let info = resp.data;
      if (!info.success) {
        alert(info.message);
        return;
      }
      selWorker.innerHTML = '';
      for (let i = 0; i < info.data.length; i++) {
        let worker = info.data[i];
        let op = document.createElement('option');
        op.append(worker.employeeName);
        op.setAttribute('value', worker.employeeId);
        selWorker.append(op);
      }
      selWorker.selectedIndex = 0;
      spWorker.innerHTML = selWorker.value;
    })
    .catch((err) => {
      console.error(err);
    });
}

selSector.addEventListener('change', () => {
  queryWorker();
});

querySector();

selWorker.addEventListener('change', () => {
  spWorker.innerHTML = selWorker.value;
});

//#endregion
